<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/7/4
 */

namespace app\student\model;


use app\course\model\CourseScheduleModel;
use think\Model;

class StudentAdjustModel extends Model
{
    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**
     * 获取学生调课信息
     * @param array $filter
     * @param array $order
     * @param int $limit
     * @param $query
     * @return \think\Paginator
     */
    public function getAdjust($filter=[], $order=[], $limit=10, $query)
    {
        //默认条件
        $where = [
            'sa.create_time' => ['>=', 0], //未删除
        ];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        //默认排序
        $sort = ['sa.id'=>'desc'];
        if(!empty($order) && is_array($order)){
            $sort = array_merge($order,$sort);
        }

        //分页url额外参数
        $page_query['query'] = [];
        if(!empty($query) && is_array($query)){
            $page_query['query'] = $query;
        }
        $adjust_data = $this->alias('sa')
            ->field('sa.*,sc.name as school_name,cl.name as class_name,co.course_name,co.course_hour,st.name as student_name')
            ->join('school sc','sa.school_id=sc.id')
            ->join('student st','sa.student_id=st.id')
            ->join('class cl','sa.class_id=cl.id')
            ->join('course co','cl.course_id=co.id')
            ->where($where)
            ->order($sort)
            ->paginate($limit,false,$page_query);

        if (count($adjust_data) > 0) {
            //获取排课
            $schedule_mdl = new CourseScheduleModel();
            $adjust_data = $adjust_data->toArray();
            foreach ($adjust_data as $k=>$v) {
                if ($v['schedule'] == 0) {
                    //是临时加课
                    $where['id'] = $v['schedule_id'];
                } else {
                    $where['schedule'] = $v['schedule'];
                }
                $where['class_id'] = $v['old_class_id'];

                $course_schedule = $schedule_mdl->where($where)->find();
                if (count($course_schedule) <= 0) {
                    unset($adjust_data[$k]);
                    continue;
                } else {
                    $new_data = [
                        'date' => date('Y-m-d', $course_schedule['date']),
                        'start_time' => $course_schedule['start_time'],
                        'end_time' => $course_schedule['end_time'],
                        'week' => $course_schedule['week'],
                        'delete_url' => url('Classes/deleteAdjust', ['id' => $v['id']], true, true),
                    ];
                    $adjust_data[$k] = array_merge($v,$new_data);
                }
            }
        }

        return $adjust_data;
    }

    /**
     * 编辑学生调课信息
     * @param array $data
     * @return bool
     */
    public function editAdjust($data=[])
    {
        $result = true;
        $id = intval($data['id']);

        $old_obj = $this->where('id', $id)->find();
        if(!isset($old_obj->id)){
            $result = false;
        }else{
            self::startTrans();
            try{
                $this->isUpdate(true)->allowField(true)->save($data, ['id' => $id]);

                self::commit();
            }catch(\Exception $e){
                self::rollback();
                $result = false;
            }
        }

        return $result;
    }

    /**
     * 通过字段获取学生调课信息
     * @param array $filter
     * @return array|false|\PDOStatement|string|Model
     */
    public function getAdjustByField($filter=[])
    {
        //默认条件
        $where = [
            'sa.create_time' => ['>=', 0],
        ];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }
        //学生调课信息
        $adjust_data = $this->alias('sa')
            ->field('sa.*,sc.name as school_name,cl.name as class_name,st.name as student_name,co.course_name,co.course_hour')
            ->join('school sc','sa.school_id=sc.id')
            ->join('class cl','sa.class_id=cl.id')
            ->join('student st','sa.student_id=st.id')
            ->join('course co','cl.course_id=co.id')
            ->where($where)
            ->find();
        if(count($adjust_data) > 0){
            $adjust_data = $adjust_data->toArray();
            if ($adjust_data['schedule'] == 0) {
                //该调课属于临时加课
                $condition['id'] = $adjust_data['schedule_id'];
            } else {
                $condition['schedule'] = $adjust_data['schedule'];
            }
            $condition['class_id'] = $adjust_data['class_id'];
            //获取排课
            $schedule_mdl = new CourseScheduleModel();
            $course_schedule = $schedule_mdl->field('date,start_time,end_time,week,classroom_id,teacher_id')
                ->where($condition)->find();
            if(count($course_schedule) > 0){
                $course_schedule = $course_schedule->toArray();
                //合并为一个数组
                $data = array_merge($adjust_data, $course_schedule);
            }else{
                $data = [];
            }

        }else{
            $data = [];
        }

        return $data;
    }

    /**
     * @param $id
     * @param $cur_time
     * @return bool
     */
    public function deleteAdjust($id, $cur_time)
    {
        $result = true;
        if(empty($id)){
            $result = false;
        }else{
            if(empty($cur_time)){
                //当前时间
                $cur_time = time();
            }
            if(is_array($id)){
                //批量删除
                self::startTrans();
                try{
                    $this->where(['id' => ['in', $id]])->delete();

                    self::commit();
                }catch(\Exception $e){
                    self::rollback();
                    $result = false;
                }
            }else{
                //单一删除
                self::startTrans();
                try{
                    $this->where(['id' => $id])->delete();

                    self::commit();
                }catch(\Exception $e){
                    self::rollback();
                    $result = false;
                }
            }
        }

        return $result;
    }

}